package com.example.mobile2021606145zbq0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

public class MainActivity extends AppCompatActivity {

    private ConstraintLayout btnMainBuy;
    private TextView tvMainTitle;
    private CheckBox ChkboxMainYinqing;
    private CheckBox ChkboxMainBoli;
    private CheckBox ChkboxMainFangxiangpan;
    private Button button;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ChkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        ChkboxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中：";
                if (ChkboxMainYinqing.isChecked()){
                    msg+="\n"+ChkboxMainYinqing.getText().toString();
                }
                if (ChkboxMainBoli.isChecked()){
                    msg+="\n"+ChkboxMainBoli.getText().toString();
                }
                if (ChkboxMainFangxiangpan.isChecked()){
                    msg+="\n"+ChkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
    }

    private void initView() {
        btnMainBuy = (ConstraintLayout) findViewById(R.id.btn_main_buy);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        ChkboxMainYinqing = (CheckBox) findViewById(R.id.Chkbox_main_yinqing);
        ChkboxMainBoli = (CheckBox) findViewById(R.id.Chkbox_main_boli);
        ChkboxMainFangxiangpan = (CheckBox) findViewById(R.id.Chkbox_main_fangxiangpan);
        button = (Button) findViewById(R.id.button);
        tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
    }
}